<template>
  <div class="login-container"></div>
</template>

<script>
import { GetAuthUrl, LoginByCode } from "@/api/wxWork";
import { Toast } from "vant";
export default {
  data() {
    return {
      AccountCode: "",
      AccountCode2: "",
      Password: "",
      OpenID: "",
      state: "",
    };
  },
  created() {
    console.log("wxwork页面");
    const self = this;
    var str = window.location.href; //获取地址栏的url

    console.log(str.substring);
    var num = str.indexOf("?");
    console.log(num);
    let arr = str.split("&"); //各个参数放到数组里
    if (arr.length > 1) {
      console.log(arr[1]);
      var b = arr[0].split("=");
      var c = b[b.length - 1]; //取到out_trade_no的值  20200515100013
      console.log(c);
    }
    // let code = this.$route.query.code;
    if (c == null) {
      this.GetAuthUrl();
    } else {
      console.log(c);
      this.AccountCode2 = c;
      //如果code有值   请求自动登录
      this.LoginByCode();
    }
  },

  mounted() {
    const { state } = this.queryToObj();
    this.state = state;
  },

  methods: {
    queryToObj() {
      const res = {};
      const pList = new URLSearchParams(location.search);
      pList.forEach((val, key) => {
        res[key] = val;
      });
      return res;
    },
    GetAuthUrl() {
      // 请求参数
      let parameter = {
        //获取当前地址
        redirectUrl: window.location.href,
      };
      GetAuthUrl(parameter).then((res) => {
        //打印输出取到的参数
        // console.log("http==========>"+res.data)
        this.AccountCode = res.data;
        window.location.href = res.data;
      });
    },
    LoginByCode() {
      // 请求参数
      // this.OpenID = this.AccountCode2
      let parameter = {
        //获取当前地址
        code: this.AccountCode2,
      };
      LoginByCode(parameter)
        .then((res) => {
          //打印输出取到的参数
          // console.log("http====data======>"+res.data)
          // onsole.log("http====OpenID======>"+res.data.OpenID)
          if (!res.success) {
            this.$toast(res.errorText);
            return;
          }
          if (res.data.OpenID) {
            this.$store.commit("updateOpenId", res.data.OpenID);
            console.log(this.$store.state.openId);
            this.$router.push("/registerPhone");
          } else {
            this.OpenID =
              res.data.UserName +
              "==ChnName=" +
              res.data.ChnName +
              "==ApplyArea=" +
              res.data.ApplyCompanyCodeArea +
              "==CompanyCode=" +
              res.data.CompanyCode +
              "==UserKind=" +
              res.data.UserKind;
            // 修改登陆状态
            this.$store.commit("updateLogin", true);
            // 把token存入store
            this.$store.commit("updateToken", res.data.UserName);
            // 把用户名存入store
            this.$store.commit("updateUserName", res.data.UserName);
            // 把用户名存入store
            this.$store.commit("updateChnName", res.data.ChnName);
            // 保存地址 updateArea
            this.$store.commit("updateArea", res.data.ApplyArea);
            // 保存地址 updateArea
            this.$store.commit("updateCompanySysType", res.data.CompanySysType);
            // 公司code
            this.$store.commit("updateCompanyCode", res.data.CompanyCode);
            this.$store
              .dispatch("getRoles", res.data.UserKind)
              .then((data) => {
                console.log(data);
              })
              .catch((_) => {
                failure("获取失败");
              });
            Toast.success("登录成功");
            setTimeout(() => {
              if (this.state == 1) {
                this.$router.push(
                  "/Folder88?num=" + Math.ceil(Math.random() * 100000)
                );
              } else if(this.state == 2){
                this.$router.push(
                  "/YatManagementDocsFX?num=" + Math.ceil(Math.random() * 100000)
                );
              }
              else {
                this.$router.push(
                  "/index?num=" + Math.ceil(Math.random() * 100000)
                );
              }
            }, 1500);
          }
        })
        .catch((_) => {
          //授权失败返回登录页面
          // Toast.fail("授权出错了");
          this.$router.push("/index");
        });
    },
  },
};
</script>

<style lang="less" scoped>
.form-box {
  background-color: #ffffff;
  height: 400px;
  border: 1px solid white;
  border-radius: 20px;
  padding: 20px 20px 0 20px;
  .phone {
    padding-left: 12px;
    margin-bottom: 10px;
  }
  .password {
    padding: 10px 0 10px 12px;
  }
}
.form-box2 {
  background-color: #ffffff;
  height: 180px;
  border: 1px solid white;
  border-radius: 20px;
  padding: 20px 20px 0 20px;
  .phone {
    padding-left: 12px;
    margin-bottom: 10px;
  }
  .password {
    padding: 10px 0 10px 12px;
  }
}
.login-container {
  background-color: #fafafa;
  height: 100vh;
  padding: 100px 20px 200px 20px;
  /*font-size: 16px;*/
  color: #000000;
}
.img-box {
  height: 100px;
  width: 100px;
  border: 1px solid #eee;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  border-radius: 50%;
  overflow: hidden;
}
</style>

